package com.jack.bookshop.dao;

import com.jack.bookshop.util.DatabaseUtil;
import com.jack.bookshop.vo.CategoryVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDao {
    public static List<CategoryVo> getListByParentID(int id){
        List<CategoryVo> list = new ArrayList<CategoryVo>();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from category where parent_id = " + id;
        try {
            conn = DatabaseUtil.getConnection();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                if(rs.getInt("parent_id")!=0){
                    CategoryVo category = new CategoryVo();
                    category.setId(rs.getInt("id"));
                    category.setText(rs.getString("text"));
                    list.add(category);
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeAll(rs, pst, conn);
        }
        return list;
    }

}
